<?php

namespace App\Admin\Controllers\Configs;

use App\Admin\Repositories\BuyMethod;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BuyMethodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BuyMethod(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('slug');
            $grid->column('price');
            $grid->column('state')->switch()->sortable();
            $grid->column('days')->sortable();
            $grid->enableDialogCreate();
            $grid->disableFilterButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BuyMethod(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('slug');
            $show->field('mark');
            $show->field('state');
            $show->field('days');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BuyMethod(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('slug');
            $form->decimal('price')->attribute('min', 0);
            $form->text('mark');
            $form->switch('state')->default(1);
            $form->number('days')->attribute('min', 0)->attribute('max', 365);
        
            $form->display('created_at');
            $form->display('updated_at');
            $form->saved(function(Form $form){
                cache()->forget('buy_methods');
            });
            $form->deleted(function(Form $form){
                cache()->forget('buy_methods');
            });
        });
    }
}
